package com.yang.booking.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yang.booking.asyncTask.BoardTask;
import com.yang.booking.dao.*;
import com.yang.booking.pojo.*;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

@Service
public class StaffService {

    @Autowired
    private StaffMapper staffMapper;

    @Autowired
    private StoreMapper storeMapper;

    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private BiweeklyMapper biweeklyMapper;

    @Autowired
    private StaffSchedulingMapper staffSchedulingMapper;

    @Autowired
    private ShiftManagementMapper shiftManagementMapper;

    @Autowired
    private AppointmentMapper appointmentMapper;

    @Autowired
    private AppointmentRuleMapper appointmentRuleMapper;

    @Autowired
    private TemporaryArrangeMapper temporaryArrangeMapper;

    @Autowired
    private ShiftManagementService shiftManagementService;

    @Autowired
    private StaffRestMapper staffRestMapper;

    @Autowired
    private BoardTask boardTask;

    public Map<String, Object> insertStaff(String storeId, String name, String position, int level, String employment,
                                           String good, String introduce, int appSituation, int onlineShopOrder, int schedulingMethod){

        Staff staff = new Staff();
        staff.setStoreId(storeId);
        staff.setName(name);
        staff.setPosition(position);
        staff.setLevel(level);
        staff.setEmployment(employment);
        staff.setGood(good);
        staff.setIntroduce(introduce);
        staff.setAppSituation(appSituation);
        staff.setOnlineShopOrder(onlineShopOrder);
        staff.setSchedulingMethod(schedulingMethod);
        staff.setCreateAt(new Date());
        staff.setUpdateAt(new Date());
        staffMapper.insert(staff);

        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "插入成功");
        msg.put("code", 200);
        return msg;
    }

    public Map<String, Object> queryStaff(String storeId, String nameOrNo, String level,String appSituation, String onlineShopOrder, String pageSize, String pageNo){

        QueryWrapper<Staff> wrapper = new QueryWrapper<>();

        if (!nameOrNo.equals("")){
            wrapper.likeRight("name", nameOrNo).or().likeLeft("name", nameOrNo).or().like("name",nameOrNo).or().
                    likeRight("no", nameOrNo).or().likeLeft("no", nameOrNo).or().like("no",nameOrNo);
        }

        if (!level.equals("")){
            wrapper.eq("level", level);
        }

        if (!appSituation.equals("")){
            wrapper.eq("app_situation", appSituation);
        }

        if (!onlineShopOrder.equals("")){
            if (onlineShopOrder == "1") {
                wrapper.ne("online_shop_order", 0);
            }else if(onlineShopOrder == "2"){
                wrapper.eq("online_shop_order", 0);
            }
        }

        wrapper.eq("store_id", storeId).eq("position", "美容师");
        Page<Staff> page = new Page<>(Long.parseLong(pageNo),Long.parseLong(pageSize));
        staffMapper.selectPage(page, wrapper);
        List<Staff> staffList = page.getRecords();

        int count = 0;
        for (Staff f: staffList) {

            f.setItemNumber(itemMapper.QueryStaffItemCount(f.getId()));

            QueryWrapper<Appointment> awrapper = new QueryWrapper<>();
            awrapper.eq("beautician_id", f.getId());
            f.setAppNumber(appointmentMapper.selectCount(awrapper));

            int repeatCount = 0;
            for (Integer t:appointmentMapper.QueryStaffItemRepeatCount(f.getId())
                 ) {
                if (t > 1) {
                    repeatCount += 1;
                }
            }
            f.setRepeat(repeatCount);

            QueryWrapper<Appointment> awrapper1 = new QueryWrapper<>();
            awrapper1.eq("beautician_id", f.getId());
            awrapper1.ne("state", 3);
            f.setService(appointmentMapper.selectCount(awrapper1));

            if (f.getAppSituation() == 1){
                count += 1;
            }
        }

        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "查询成功");
        msg.put("code", 200);
        Map<String, Object> data = new HashMap<String, Object>();
        msg.put("data", data);
        data.put("rows", staffList);
        data.put("total", staffList.size());
        data.put("count", count);
        return msg;
    }

    public Map<String, Object> updateStaffAppSituation(String staffIdList, String appSituation){

        for (String staffId: staffIdList.split(",")) {
            Staff staff = new Staff();
            staff.setId(staffId);
            staff.setAppSituation(Integer.parseInt(appSituation));
            staffMapper.updateById(staff);
        }

        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "更新成功");
        msg.put("code", 200);
        return msg;
    }

    public Map<String, Object> updateStaffInfo(String staffId, String name, String employment, String good, String introduce){

        Staff staff = new Staff();
        staff.setId(staffId);
        staff.setName(name);
        staff.setEmployment(employment);
        staff.setGood(good);
        staff.setIntroduce(introduce);
        staffMapper.updateById(staff);

        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "更新成功");
        msg.put("code", 200);
        return msg;
    }

    public Map<String, Object> queryStaffScheduling(String storeId, String nameOrNo, String level, String pageSize, String pageNo){

        QueryWrapper<Staff> wrapper = new QueryWrapper<>();

        if (!nameOrNo.equals("")){
            wrapper.likeRight("name", nameOrNo).or().likeLeft("name", nameOrNo).or().like("name",nameOrNo).or().
                    likeRight("no", nameOrNo).or().likeLeft("no", nameOrNo).or().like("no",nameOrNo);
        }

        if (!level.equals("")){
            wrapper.eq("level", level);
        }

        wrapper.eq("store_id", storeId).eq("position", "美容师");
        Page<Staff> page = new Page<>(Long.parseLong(pageNo),Long.parseLong(pageSize));
        staffMapper.selectPage(page, wrapper);
        List<Staff> staffList = page.getRecords();

        Store s = storeMapper.selectById(storeId);

        for (Staff f: staffList) {
            QueryWrapper<Biweekly> bwrapper = new QueryWrapper<>();
            bwrapper.eq("type", s.getSchedulingCycle());
            bwrapper.orderByAsc("create_at");
            List<Biweekly> biweeklyList = biweeklyMapper.selectList(bwrapper);

            for (Biweekly b: biweeklyList) {
                ShiftManagement shiftManagement = shiftManagementMapper.queryStaffBiweeklyShift(f.getId(), b.getId());
                if (shiftManagement != null){
                    b.setShiftManagement(shiftManagement);
                }else {
                    ShiftManagement shiftManagement1 = new ShiftManagement();
                    shiftManagement1.setName("休息");
                    b.setShiftManagement(shiftManagement1);
                }
            }

            f.setBiweeklyList(biweeklyList);
        }

        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "查询成功");
        msg.put("code", 200);
        Map<String, Object> data = new HashMap<String, Object>();
        msg.put("data", data);
        data.put("rows", staffList);
        data.put("total", staffList.size());
        return msg;
    }

    @Transactional
    public Map<String, Object> insertStaffSchedling(String storeId, String staffIdList, String schedlingMothed, String frequencyId, String biweeklyList){

        for (String staffId: staffIdList.split(",")) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("staff_id", staffId);
            staffSchedulingMapper.deleteByMap(map);

            Staff s = staffMapper.selectById(staffId);
            s.setSchedulingMethod(Integer.parseInt(schedlingMothed));
            staffMapper.updateById(s);

            if(schedlingMothed.equals("1")){
                List<Biweekly> biweeklies = JSONObject.parseArray(biweeklyList, Biweekly.class);
                for (Biweekly biweekly : biweeklies) {
                    if (biweekly.getShiftManagement().getName().equals("休息")){
                        continue;
                    }
                    StaffScheduling ss = new StaffScheduling();
                    ss.setStaffId(staffId);
                    ss.setBiweeklyId(biweekly.getId());
                    ss.setShiftManagementId(biweekly.getShiftManagement().getId());
                    ss.setCreateAt(new Date());
                    ss.setUpdateAt(new Date());
                    staffSchedulingMapper.insert(ss);
                }
            }

            if(schedlingMothed.equals("0")){
                Store store = storeMapper.selectById(storeId);
                HashMap<String, Object> map1 = new HashMap<>();
                map1.put("type", store.getSchedulingCycle());
                List<Biweekly> biweeklies1 = biweeklyMapper.selectByMap(map1);
                for (Biweekly biweekly : biweeklies1) {
                    if (frequencyId.equals("")){
                        continue;
                    }
                    StaffScheduling ss = new StaffScheduling();
                    ss.setStaffId(staffId);
                    ss.setBiweeklyId(biweekly.getId());
                    ss.setShiftManagementId(frequencyId);
                    ss.setCreateAt(new Date());
                    ss.setUpdateAt(new Date());
                    staffSchedulingMapper.insert(ss);
                }
            }
        }

        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "插入成功");
        msg.put("code", 200);
        return msg;
    }

    public Map<String, Object> queryStaffBoard(String storeId, String arrivalDate) throws ParseException {

        QueryWrapper<AppointmentRule> wrapper = new QueryWrapper<>();
        wrapper.eq("store_id", storeId);
        AppointmentRule appointmentRule = appointmentRuleMapper.selectOne(wrapper);

        QueryWrapper<Staff> swrapper = new QueryWrapper<>();
        swrapper.eq("store_id", storeId);
        swrapper.eq("app_situation", 1);
        swrapper.eq("position", "美容师");
        int scount = staffMapper.selectCount(swrapper);
        List<Staff> staffList = staffMapper.selectList(swrapper);

        List<TimeSlot> timeSlotList = new ArrayList<>();
        for (int j = 9; j <= 18; j++) {
            if (j == 18) {
                TimeSlot timeSlot = new TimeSlot();
                timeSlot.setName(Integer.toString(j) + ":00");
                timeSlotList.add(timeSlot);
            } else {
                String name;
                if (j == 9) {
                    name = "0" + Integer.toString(j);
                } else {
                    name = Integer.toString(j);
                }
                TimeSlot timeSlot = new TimeSlot();
                timeSlot.setName(name + ":00");
                timeSlotList.add(timeSlot);

                TimeSlot timeSlot1 = new TimeSlot();
                timeSlot1.setName(name + ":30");
                timeSlotList.add(timeSlot1);
            }
        }
        for (TimeSlot t : timeSlotList
        ) {
            QueryWrapper<Appointment> awrapper1 = new QueryWrapper<>();
            awrapper1.eq("store_id", storeId);
            awrapper1.eq("type", 2);
            awrapper1.le("complete_service_start_time", arrivalDate + " " + t.getName() + ":00");
            awrapper1.gt("complete_service_end_time", arrivalDate + " " + t.getName() + ":00");
            awrapper1.ne("state",3);
            t.setArrangement(appointmentMapper.selectCount(awrapper1));
            t.setArrangementAppointmentList(appointmentMapper.selectList(awrapper1));

            QueryWrapper<Appointment> awrapper = new QueryWrapper<>();
            awrapper.eq("store_id", storeId);
            awrapper.ne("type", 3);
            awrapper.le("complete_service_start_time", arrivalDate + " " + t.getName() + ":00");
            awrapper.gt("complete_service_end_time", arrivalDate + " " + t.getName() + ":00");
            awrapper.ne("state",3);
            int acount = appointmentMapper.selectCount(awrapper);

            int rcount = 0;
            int ecount = 0;
            for (Staff staff : staffList
            ) {
                boolean tem = true;
                for (String staffShiftTime : shiftManagementService.queryStaffShift(storeId, staff.getId(), arrivalDate, "2", "1")
                ) {
                    if (staffShiftTime.equals(t.getName())) {
                        tem = false;
                        break;
                    }
                }
                if(tem){
                    rcount += 1;
                }

                QueryWrapper<StaffRest> rwrapper = new QueryWrapper<>();
                rwrapper.eq("staff_id", staff.getId());
                rwrapper.eq("rest_date", arrivalDate);
                rwrapper.eq("rest_time", t.getName());
                if (staffRestMapper.selectOne(rwrapper) != null){
                    ecount += 1;
                }
            }
            if (appointmentRule != null && appointmentRule.getMostMeanwhile() != 0) {
                if (appointmentRule.getMostMeanwhile() > acount + rcount + ecount) {
                    t.setResidualReducibility(Integer.toString(appointmentRule.getMostMeanwhile() - rcount - acount - ecount));
                } else {
                    t.setResidualReducibility("满");
                }
            } else {
                if (scount > acount + rcount + ecount) {
                    t.setResidualReducibility(Integer.toString(scount -rcount - acount - ecount));
                } else {
                    t.setResidualReducibility("满");
                }
            }

            List<Staff> staffList1 = staffMapper.selectList(swrapper);
            for (Staff staff1 : staffList1
            ) {
                boolean tem = true;
                for (String staffShiftTime : shiftManagementService.queryStaffShift(storeId, staff1.getId(), arrivalDate, "2", "1")
                ) {
                    if(staffShiftTime.equals(t.getName())){
                        tem = false;
                        break;
                    }
                }
                if(tem){
                    staff1.setCurrentBoardStatus(2);
                }

                QueryWrapper<StaffRest> rwrapper = new QueryWrapper<>();
                rwrapper.eq("staff_id", staff1.getId());
                rwrapper.eq("rest_date", arrivalDate);
                rwrapper.eq("rest_time", t.getName());
                StaffRest staffRest = staffRestMapper.selectOne(rwrapper);
                if (staffRest != null) {
                    staff1.setCurrentBoardStatus(3);
                }

                QueryWrapper<Appointment> sawrapper = new QueryWrapper<>();
                sawrapper.eq("beautician_id", staff1.getId());
                sawrapper.eq("type", 1);
                sawrapper.le("complete_service_start_time", arrivalDate + " " + t.getName() + ":00");
                sawrapper.gt("complete_service_end_time", arrivalDate + " " + t.getName() + ":00");
                sawrapper.ne("state",3);
                Appointment appointment = appointmentMapper.selectOne(sawrapper);
                if (appointment != null){
                    staff1.setCurrentBoardStatus(1);
                    staff1.setAppointment(appointment);

                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
                    Date start = simpleDateFormat.parse(appointment.getServiceStartTime());
                    Date end = simpleDateFormat.parse(appointment.getServiceEndTime());
                    appointment.setOccupiedCells((int) ((end.getTime() - start.getTime()) / 60 / 1000) / 30);
                }else {
                    if (t.getResidualReducibility().equals("满") && staff1.getCurrentBoardStatus() != 2){
                        staff1.setCurrentBoardStatus(4);
                    }
                }

//                if (staff.getCurrentBoardStatus() == 0){
//                    System.out.println("空闲");
//                }else if (staff.getCurrentBoardStatus() == 1){
//                    System.out.println("被预约");
//                }else if(staff.getCurrentBoardStatus() == 2) {
//                    System.out.println("排班休息");
//                }else if(staff.getCurrentBoardStatus() == 3) {
//                    System.out.println("上班休息");
//                }else {
//                    System.out.println("已满客");
//                }

            }
            t.setStaffList(staffList1);

            HashMap<String, Object> tmap = new HashMap<>();
            tmap.put("store_id", storeId);
            List<TemporaryArrange> temporaryArrangeList = temporaryArrangeMapper.selectByMap(tmap);
            for (TemporaryArrange temporaryArrange : temporaryArrangeList
            ) {
                QueryWrapper<Appointment> twrapper = new QueryWrapper<>();
                twrapper.eq("type", 3);
                twrapper.eq("temporary_arrange_id", temporaryArrange.getId());
                twrapper.le("complete_service_start_time", arrivalDate + " " + t.getName() + ":00");
                twrapper.gt("complete_service_end_time", arrivalDate + " " + t.getName() + ":00");
                twrapper.ne("state",3);
                Appointment appointment = appointmentMapper.selectOne(twrapper);
                temporaryArrange.setAppointment(appointment);

                if (appointment != null) {
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
                    Date start = simpleDateFormat.parse(appointment.getServiceStartTime());
                    Date end = simpleDateFormat.parse(appointment.getServiceEndTime());
                    appointment.setOccupiedCells((int) ((end.getTime() - start.getTime()) / 60 / 1000) / 30);
                }
            }
            t.setTemporaryArrangeList(temporaryArrangeList);
        }

        QueryWrapper<Appointment> nwrapper = new QueryWrapper<>();
        nwrapper.eq("store_id", storeId);
        nwrapper.eq("state", 1);

        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "查询成功");
        msg.put("code", 200);
        Map<String, Object> data = new HashMap<String, Object>();
        msg.put("data", data);
        data.put("rows", timeSlotList);
        data.put("noBillToday", appointmentMapper.selectCount(nwrapper));
        return msg;

        //方式一
//        QueryWrapper<AppointmentRule> wrapper = new QueryWrapper<>();
//        wrapper.eq("store_id", storeId);
//        AppointmentRule appointmentRule = appointmentRuleMapper.selectOne(wrapper);
//
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("store_id", storeId);
//        map.put("app_situation", 1);
//        map.put("position", "美容师");
//        List<Staff> staffList = staffMapper.selectByMap(map);
//        for (Staff staff :staffList
//             ) {
//            List<TimeSlot> timeSlotList = new ArrayList<>();
//            for(int j=9; j <= 18; j ++){
//                if(j == 18){
//                    TimeSlot timeSlot = new TimeSlot();
//                    timeSlot.setName(Integer.toString(j)+":00");
//                    timeSlotList.add(timeSlot);
//                }else {
//                    String name;
//                    if (j == 9 ){
//                       name = "0"+Integer.toString(j);
//                    }else {
//                        name = Integer.toString(j);
//                    }
//                    TimeSlot timeSlot = new TimeSlot();
//                    timeSlot.setName(name + ":00");
//                    timeSlotList.add(timeSlot);
//
//                    TimeSlot timeSlot1 = new TimeSlot();
//                    timeSlot.setName(name + ":30");
//                    timeSlotList.add(timeSlot1);
//                }
//            }
//            for (TimeSlot t :timeSlotList
//                 ) {
//                HashMap<String, Object> map1 = new HashMap<>();
//                map1.put("beautician_id", staff.getId());
//                map1.put("arrival_date", arrivalDate);
//                map1.put("service_start_time", t.getName());
//                map1.put("type", 1);
//                t.setAppointmentList(appointmentMapper.selectByMap(map1));
//
//                for (Appointment appointment: t.getAppointmentList()
//                     ) {
//                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
//                    Date start = simpleDateFormat.parse(appointment.getServiceStartTime());
//                    Date end = simpleDateFormat.parse(appointment.getServiceEndTime());
//                    appointment.setOccupiedCells((int)((end.getTime()-start.getTime())/60/1000)/30);
//                }
//
//                QueryWrapper<Appointment> awrapper = new QueryWrapper<>();
//                awrapper.eq("store_id", storeId);
//                awrapper.ne("type", 3);
//                awrapper.le("complete_service_start_time", arrivalDate+" "+t.getName()+":00");
////                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
////                Date start = simpleDateFormat.parse(arrivalDate+t.getName()+":00");
////                String end = simpleDateFormat.format(new Date(start.getTime()+30*60*1000));
//                awrapper.gt("complete_service_ent_time", arrivalDate+" "+t.getName()+":00");
//                int acount = appointmentMapper.selectCount(awrapper);
//
//                QueryWrapper<Staff> swrapper = new QueryWrapper<>();
//                swrapper.eq("store_id", storeId);
//                swrapper.eq("app_situation", 1);
//                swrapper.eq("position", "美容师");
//                int scount = staffMapper.selectCount(swrapper);
//
//                QueryWrapper<Appointment> awrapper1 = new QueryWrapper<>();
//                awrapper1.eq("store_id", storeId);
//                awrapper1.eq("type", 2);
//                awrapper1.le("complete_service_start_time", arrivalDate+" "+t.getName()+":00");
//                awrapper1.gt("complete_service_ent_time", arrivalDate+" "+t.getName()+":00");
//                int acount1 = appointmentMapper.selectCount(awrapper1);
//                t.setArrangement(acount1);
//
//                if(appointmentRule.getMostMeanwhile() != 0) {
//                    if (appointmentRule.getMostMeanwhile() > acount) {
//                        t.setResidualReducibility(Integer.toString(appointmentRule.getMostMeanwhile() - acount));
//                    }else {
//                        t.setResidualReducibility("满");
//                        for (Staff staffProhibit : staffList
//                        ) {
//                            HashMap<String, Object> map2 = new HashMap<>();
//                            map2.put("beautician_id", staff.getId());
//                            map2.put("arrival_date", arrivalDate);
//                            map2.put("service_start_time", t.getName());
//                            map2.put("type", 1);
//                            if (appointmentMapper.selectByMap(map2) == null) {
//                                staffProhibit.setProhibit(true);
//                            }
//                        }
//                    }
//                }else {
//                    if (scount > acount) {
//                        t.setResidualReducibility(Integer.toString(scount - acount));
//                    }else {
//                        t.setResidualReducibility("满");
//                        for (Staff staffProhibit : staffList
//                        ) {
//                            HashMap<String, Object> map2 = new HashMap<>();
//                            map2.put("beautician_id", staff.getId());
//                            map2.put("arrival_date", arrivalDate);
//                            map2.put("service_start_time", t.getName());
//                            map2.put("type", 1);
//                            if (appointmentMapper.selectByMap(map2) == null) {
//                                staffProhibit.setProhibit(true);
//                            }
//                        }
//                    }
//                }
//
//                boolean tem = true;
//                for (String staffShiftTime:shiftManagementService.queryStaffShift(storeId, staff.getId(), arrivalDate, "2", "1")
//                ) {
//                    if(staffShiftTime == t.getName()){
//                        tem = false;
//                        break;
//                    }
//                }
//                if(tem){
//                    t.setRest(true);
//                }
//            }
//            staff.setTimeSlotList(timeSlotList);
//        }
//
//        HashMap<String, Object> tmap = new HashMap<>();
//        tmap.put("store_id", storeId);
//        List<TemporaryArrange> temporaryArrangeList = temporaryArrangeMapper.selectByMap(tmap);
//        for (TemporaryArrange temporaryArrange : temporaryArrangeList
//        ) {
//            List<TimeSlot> timeSlotList = new ArrayList<>();
//            for (int j = 9; j <= 18; j++) {
//                if (j == 18) {
//                    TimeSlot timeSlot = new TimeSlot();
//                    timeSlot.setName(Integer.toString(j) + ":00");
//                    timeSlotList.add(timeSlot);
//                } else {
//                    String name;
//                    if (j == 9 ){
//                        name = "0"+Integer.toString(j);
//                    }else {
//                        name = Integer.toString(j);
//                    }
//                    TimeSlot timeSlot = new TimeSlot();
//                    timeSlot.setName(name + ":00");
//                    timeSlotList.add(timeSlot);
//
//                    TimeSlot timeSlot1 = new TimeSlot();
//                    timeSlot.setName(name + ":30");
//                    timeSlotList.add(timeSlot1);
//                }
//            }
//            for (TimeSlot t : timeSlotList
//            ) {
//                HashMap<String, Object> map1 = new HashMap<>();
//                map1.put("arrival_date", arrivalDate);
//                map1.put("type", 3);
//                map1.put("temporary_arrange_id", temporaryArrange.getId());
//                map1.put("service_start_time", t.getName());
//                t.setAppointmentList(appointmentMapper.selectByMap(map1));
//
//                for (Appointment appointment : t.getAppointmentList()
//                ) {
//                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
//                    Date start = simpleDateFormat.parse(appointment.getServiceStartTime());
//                    Date end = simpleDateFormat.parse(appointment.getServiceEndTime());
//                    appointment.setOccupiedCells((int) ((end.getTime() - start.getTime()) / 60 / 1000) / 30);
//                }
//            }
//            temporaryArrange.setTimeSlotList(timeSlotList);
//        }
//
//        Map<String, Object> msg = new HashMap<String, Object>();
//        msg.put("massage", "查询成功");
//        msg.put("code", 200);
//        Map<String, Object> data = new HashMap<String, Object>();
//        Map<String, Object> arrange = new HashMap<String, Object>();
//        msg.put("data", data);
//        data.put("rows", arrange);
//        arrange.put("staffArrange", staffList);
//        arrange.put("temporaryArrange", temporaryArrangeList);
//
//        return msg;
    }

    public Map<String, Object> queryStaffBoardNew(String storeId, String arrivalDate) throws ParseException {

        QueryWrapper<AppointmentRule> wrapper = new QueryWrapper<>();
        wrapper.eq("store_id", storeId);
        AppointmentRule appointmentRule = appointmentRuleMapper.selectOne(wrapper);

        QueryWrapper<Staff> swrapper = new QueryWrapper<>();
        swrapper.eq("store_id", storeId);
        swrapper.eq("app_situation", 1);
        swrapper.eq("position", "美容师");
        int scount = staffMapper.selectCount(swrapper);
        List<Staff> staffList = staffMapper.selectList(swrapper);

        HashMap<String, Object> tmap = new HashMap<>();
        tmap.put("store_id", storeId);
        List<TemporaryArrange> temporaryArrangeList = temporaryArrangeMapper.selectByMap(tmap);

        QueryWrapper<Appointment> sawrapper = new QueryWrapper<>();
        sawrapper.eq("store_id", storeId);
        sawrapper.eq("arrival_date", arrivalDate);
        sawrapper.ne("state",3);
        List<Appointment> appointmentList = appointmentMapper.selectList(sawrapper);

        QueryWrapper<StaffRest> rwrapper1 = new QueryWrapper<>();
        rwrapper1.eq("store_id", storeId);
        rwrapper1.eq("rest_date", arrivalDate);
        List<StaffRest> staffRestList = staffRestMapper.selectList(rwrapper1);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");

        List<TimeSlot> timeSlotList = new ArrayList<>();
        for (int j = 9; j <= 18; j++) {
            if (j == 18) {
                TimeSlot timeSlot = new TimeSlot();
                timeSlot.setName(Integer.toString(j) + ":00");
                timeSlotList.add(timeSlot);
            } else {
                String name;
                if (j == 9) {
                    name = "0" + Integer.toString(j);
                } else {
                    name = Integer.toString(j);
                }
                TimeSlot timeSlot = new TimeSlot();
                timeSlot.setName(name + ":00");
                timeSlotList.add(timeSlot);

                TimeSlot timeSlot1 = new TimeSlot();
                timeSlot1.setName(name + ":30");
                timeSlotList.add(timeSlot1);
            }
        }
        for (TimeSlot t : timeSlotList
        ) {
            int taacount = 0;
            int acount =  0;
            for (Appointment appointment: appointmentList
                 ) {
                if (appointment.getType() == 2 && (simpleDateFormat.parse(t.getName()).getTime() >= simpleDateFormat.parse(appointment.getServiceStartTime()).getTime() &&
                        simpleDateFormat.parse(t.getName()).getTime() < simpleDateFormat.parse(appointment.getServiceEndTime()).getTime())){
                    taacount += 1;
                    t.getArrangementAppointmentList().add(appointment);
                }

                if (appointment.getType() != 3 && (simpleDateFormat.parse(t.getName()).getTime() >= simpleDateFormat.parse(appointment.getServiceStartTime()).getTime() &&
                        simpleDateFormat.parse(t.getName()).getTime() < simpleDateFormat.parse(appointment.getServiceEndTime()).getTime())){
                    acount += 1;
                }
            }
            t.setArrangement(taacount);

            int rcount = 0;
            int ecount = 0;
            List<Map<String, Object>> staffMapList = new ArrayList<>();
            for (Staff staff: staffList
                 ) {
                Map<String, Object> staffMap = new HashMap<>();
                staffMap.put("staffId", staff.getId());
                staffMap.put("staffName", staff.getName());
                staffMap.put("currentBoardStatus", 0);
                staffMap.put("appointment", null);
                staffMapList.add(staffMap);
            }
            for (Map<String, Object> staffMap : staffMapList
            ) {
                boolean tem = true;
                for (String staffShiftTime : shiftManagementService.queryStaffShift(storeId, (String) staffMap.get("staffId"), arrivalDate, "2", "1")
                ) {
                    if (staffShiftTime.equals(t.getName())) {
                        tem = false;
                        break;
                    }
                }
                if (tem) {
                    rcount += 1;
                    staffMap.put("currentBoardStatus", 2);
                }

                for (StaffRest staffRest : staffRestList
                ) {
                    if (staffRest.getStaffId().equals((String) staffMap.get("staffId")) && staffRest.getRestTime().equals(t.getName())) {
                        ecount += 1;
                        staffMap.put("currentBoardStatus", 3);
                    }
                }

                if (appointmentRule != null && appointmentRule.getMostMeanwhile() != 0) {
                    if (appointmentRule.getMostMeanwhile() > acount + rcount + ecount) {
                        t.setResidualReducibility(Integer.toString(appointmentRule.getMostMeanwhile() - rcount - acount - ecount));
                    } else {
                        t.setResidualReducibility("满");
                    }
                } else {
                    if (scount > acount + rcount + ecount) {
                        t.setResidualReducibility(Integer.toString(scount - rcount - acount - ecount));
                    } else {
                        t.setResidualReducibility("满");
                    }
                }

                for (Appointment appointment : appointmentList
                ) {
                    if (appointment.getBeauticianId().equals((String) staffMap.get("staffId")) && appointment.getType() == 1 && (simpleDateFormat.parse(t.getName()).getTime() >= simpleDateFormat.parse(appointment.getServiceStartTime()).getTime() &&
                            simpleDateFormat.parse(t.getName()).getTime() < simpleDateFormat.parse(appointment.getServiceEndTime()).getTime())) {

                        staffMap.put("currentBoardStatus", 1);
                        staffMap.put("appointment", appointment);

                        Date start = simpleDateFormat.parse(appointment.getServiceStartTime());
                        Date end = simpleDateFormat.parse(appointment.getServiceEndTime());
                        appointment.setOccupiedCells((int) ((end.getTime() - start.getTime()) / 60 / 1000) / 30);
                    }
                }

                if (t.getResidualReducibility().equals("满")) {
                    if ((int) staffMap.get("currentBoardStatus") != 2 && (int) staffMap.get("currentBoardStatus") != 1) {
                        staffMap.put("currentBoardStatus", 4);
                    }
                }
            }
            t.setStaffMaplist(staffMapList);

            List<Map<String, Object>> temporaryArrangeMapList = new ArrayList<>();
            for (TemporaryArrange temporaryArrange: temporaryArrangeList
            ) {
                Map<String, Object> temporaryArrangeMap = new HashMap<>();
                temporaryArrangeMap.put("temporaryArrangeId", temporaryArrange.getId());
                temporaryArrangeMap.put("temporaryArrangeName", temporaryArrange.getName());
                temporaryArrangeMap.put("appointment", null);
                temporaryArrangeMapList.add(temporaryArrangeMap);
            }
            for (Map<String, Object> temporaryArrangeMap : temporaryArrangeMapList
            ) {
                for (Appointment appointment : appointmentList
                ) {
                    if (appointment.getTemporaryArrangeId() != null) {
                        if (appointment.getTemporaryArrangeId().equals((String) temporaryArrangeMap.get("temporaryArrangeId")) && (simpleDateFormat.parse(t.getName()).getTime() >= simpleDateFormat.parse(appointment.getServiceStartTime()).getTime() &&
                                simpleDateFormat.parse(t.getName()).getTime() < simpleDateFormat.parse(appointment.getServiceEndTime()).getTime())) {
                            temporaryArrangeMap.put("appointment", appointment);

                            Date start = simpleDateFormat.parse(appointment.getServiceStartTime());
                            Date end = simpleDateFormat.parse(appointment.getServiceEndTime());
                            appointment.setOccupiedCells((int) ((end.getTime() - start.getTime()) / 60 / 1000) / 30);
                        }
                    }
                }
            }
            t.setTemporaryArrangeMapList(temporaryArrangeMapList);
        }

        QueryWrapper<Appointment> nwrapper = new QueryWrapper<>();
        nwrapper.eq("store_id", storeId);
        nwrapper.eq("state", 1);

        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "查询成功");
        msg.put("code", 200);
        Map<String, Object> data = new HashMap<String, Object>();
        msg.put("data", data);
        data.put("rows", timeSlotList);
        data.put("noBillToday", appointmentMapper.selectCount(nwrapper));
        return msg;
    }

    public Map<String, Object> queryStaffBoardAsync(String storeId, String arrivalDate) throws InterruptedException {

        QueryWrapper<AppointmentRule> wrapper = new QueryWrapper<>();
        wrapper.eq("store_id", storeId);
        AppointmentRule appointmentRule = appointmentRuleMapper.selectOne(wrapper);

        QueryWrapper<Staff> swrapper = new QueryWrapper<>();
        swrapper.eq("store_id", storeId);
        swrapper.eq("app_situation", 1);
        swrapper.eq("position", "美容师");
        int scount = staffMapper.selectCount(swrapper);
        List<Staff> staffList = staffMapper.selectList(swrapper);

        HashMap<String, Object> tmap = new HashMap<>();
        tmap.put("store_id", storeId);
        List<TemporaryArrange> temporaryArrangeList = temporaryArrangeMapper.selectByMap(tmap);

        QueryWrapper<Appointment> sawrapper = new QueryWrapper<>();
        sawrapper.eq("store_id", storeId);
        sawrapper.eq("arrival_date", arrivalDate);
        sawrapper.ne("state",3);
        List<Appointment> appointmentList = appointmentMapper.selectList(sawrapper);

        QueryWrapper<StaffRest> rwrapper1 = new QueryWrapper<>();
        rwrapper1.eq("store_id", storeId);
        rwrapper1.eq("rest_date", arrivalDate);
        List<StaffRest> staffRestList = staffRestMapper.selectList(rwrapper1);

        List<TimeSlot> timeSlotList = new ArrayList<>();
        for (int j = 9; j <= 18; j++) {
            if (j == 18) {
                TimeSlot timeSlot = new TimeSlot();
                timeSlot.setName(Integer.toString(j) + ":00");
                timeSlotList.add(timeSlot);
            } else {
                String name;
                if (j == 9) {
                    name = "0" + Integer.toString(j);
                } else {
                    name = Integer.toString(j);
                }
                TimeSlot timeSlot = new TimeSlot();
                timeSlot.setName(name + ":00");
                timeSlotList.add(timeSlot);

                TimeSlot timeSlot1 = new TimeSlot();
                timeSlot1.setName(name + ":30");
                timeSlotList.add(timeSlot1);
            }
        }

//        ReentrantLock reentrantLock = new ReentrantLock();
        CountDownLatch latch = new CountDownLatch(7);
        ThreadPoolExecutor executor = new ThreadPoolExecutor(7, 8, 1L, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(8));
        for (int i = 0; i < 7; i++) {
            int p = i;
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");

            executor.execute(() -> {
                List<TimeSlot> subTimeSlotList = new ArrayList<>();
                if (p >= 6){
                    subTimeSlotList = timeSlotList.subList(p*3, timeSlotList.size());
                }else {
                    subTimeSlotList = timeSlotList.subList(p*3, (p+1)*3);
                }

//                subTimeSlotList = timeSlotList.subList(p, p+1);
                for (TimeSlot t : subTimeSlotList
                ) {
                    int taacount = 0;
                    int acount =  0;
                    for (Appointment appointment: appointmentList
                    ) {
                        try {
                            if (appointment.getType() == 2 && (simpleDateFormat.parse(t.getName()).getTime() >= simpleDateFormat.parse(appointment.getServiceStartTime()).getTime() &&
                                    simpleDateFormat.parse(t.getName()).getTime() < simpleDateFormat.parse(appointment.getServiceEndTime()).getTime())){
                                taacount += 1;
                                t.getArrangementAppointmentList().add(appointment);
                            }
                        } catch (ParseException e) {
                            latch.countDown();
                            e.printStackTrace();
                        }

                        try {
                            if (appointment.getType() != 3 && (simpleDateFormat.parse(t.getName()).getTime() >= simpleDateFormat.parse(appointment.getServiceStartTime()).getTime() &&
                                    simpleDateFormat.parse(t.getName()).getTime() < simpleDateFormat.parse(appointment.getServiceEndTime()).getTime())){
                                acount += 1;
                            }
                        } catch (ParseException e) {
                            latch.countDown();
                            e.printStackTrace();
                        }
                    }
                    t.setArrangement(taacount);

                    int rcount = 0;
                    int ecount = 0;
                    List<Map<String, Object>> staffMapList = new ArrayList<>();
                    for (Staff staff: staffList
                    ) {
                        Map<String, Object> staffMap = new HashMap<>();
                        staffMap.put("staffId", staff.getId());
                        staffMap.put("staffName", staff.getName());
                        staffMap.put("currentBoardStatus", 0);
                        staffMap.put("appointment", null);
                        staffMapList.add(staffMap);
                    }
                    for (Map<String, Object> staffMap : staffMapList
                    ) {
                        boolean tem = true;
                        try {
                            for (String staffShiftTime : shiftManagementService.queryStaffShift(storeId, (String) staffMap.get("staffId"), arrivalDate, "2", "1")
                            ) {
                                if (staffShiftTime.equals(t.getName())) {
                                    tem = false;
                                    break;
                                }
                            }
                        } catch (ParseException e) {
                            latch.countDown();
                            e.printStackTrace();
                        }
                        if (tem) {
                            rcount += 1;
                            staffMap.put("currentBoardStatus", 2);
                        }

                        for (StaffRest staffRest : staffRestList
                        ) {
                            if (staffRest.getStaffId().equals((String) staffMap.get("staffId")) && staffRest.getRestTime().equals(t.getName())) {
                                ecount += 1;
                                staffMap.put("currentBoardStatus", 3);
                            }
                        }

                        if (appointmentRule != null && appointmentRule.getMostMeanwhile() != 0) {
                            if (appointmentRule.getMostMeanwhile() > acount + rcount + ecount) {
                                t.setResidualReducibility(Integer.toString(appointmentRule.getMostMeanwhile() - rcount - acount - ecount));
                            } else {
                                t.setResidualReducibility("满");
                            }
                        } else {
                            if (scount > acount + rcount + ecount) {
                                t.setResidualReducibility(Integer.toString(scount - rcount - acount - ecount));
                            } else {
                                t.setResidualReducibility("满");
                            }
                        }

                        for (Appointment appointment : appointmentList
                        ) {
                            try {
                                if (appointment.getBeauticianId().equals((String) staffMap.get("staffId")) && appointment.getType() == 1 && (simpleDateFormat.parse(t.getName()).getTime() >= simpleDateFormat.parse(appointment.getServiceStartTime()).getTime() &&
                                        simpleDateFormat.parse(t.getName()).getTime() < simpleDateFormat.parse(appointment.getServiceEndTime()).getTime())) {

                                    staffMap.put("currentBoardStatus", 1);
                                    staffMap.put("appointment", appointment);

                                    Date start = simpleDateFormat.parse(appointment.getServiceStartTime());
                                    Date end = simpleDateFormat.parse(appointment.getServiceEndTime());

//                                    if (appointment.getOccupiedCells() == 0) {
//                                    reentrantLock.lock();
                                    appointment.setOccupiedCells((int) ((end.getTime() - start.getTime()) / 60 / 1000) / 30);
//                                    reentrantLock.unlock();
//                                    }
                                }
                            } catch (ParseException e) {
                                latch.countDown();
                                e.printStackTrace();
                            }
                        }

                        if (t.getResidualReducibility().equals("满")) {
                            if ((int) staffMap.get("currentBoardStatus") != 2 && (int) staffMap.get("currentBoardStatus") != 1) {
                                staffMap.put("currentBoardStatus", 4);
                            }
                        }
                    }
                    t.setStaffMaplist(staffMapList);

                    List<Map<String, Object>> temporaryArrangeMapList = new ArrayList<>();
                    for (TemporaryArrange temporaryArrange: temporaryArrangeList
                    ) {
                        Map<String, Object> temporaryArrangeMap = new HashMap<>();
                        temporaryArrangeMap.put("temporaryArrangeId", temporaryArrange.getId());
                        temporaryArrangeMap.put("temporaryArrangeName", temporaryArrange.getName());
                        temporaryArrangeMap.put("appointment", null);
                        temporaryArrangeMapList.add(temporaryArrangeMap);
                    }
                    for (Map<String, Object> temporaryArrangeMap : temporaryArrangeMapList
                    ) {
                        for (Appointment appointment : appointmentList
                        ) {
                            if (appointment.getTemporaryArrangeId() != null) {
                                try {
                                    if (appointment.getTemporaryArrangeId().equals((String) temporaryArrangeMap.get("temporaryArrangeId")) && (simpleDateFormat.parse(t.getName()).getTime() >= simpleDateFormat.parse(appointment.getServiceStartTime()).getTime() &&
                                            simpleDateFormat.parse(t.getName()).getTime() < simpleDateFormat.parse(appointment.getServiceEndTime()).getTime())) {
                                        temporaryArrangeMap.put("appointment", appointment);

                                        Date start = simpleDateFormat.parse(appointment.getServiceStartTime());
                                        Date end = simpleDateFormat.parse(appointment.getServiceEndTime());

//                                        if (appointment.getOccupiedCells() == 0) {
//                                        reentrantLock.lock();
                                        appointment.setOccupiedCells((int) ((end.getTime() - start.getTime()) / 60 / 1000) / 30);
//                                        reentrantLock.unlock();
//                                        }
                                    }
                                } catch (ParseException e) {
                                    latch.countDown();
                                    e.printStackTrace();
                                }
                            }
                        }
                    }
                    t.setTemporaryArrangeMapList(temporaryArrangeMapList);
                }

                latch.countDown();
            });
        }
        latch.await();
        executor.shutdown();

        QueryWrapper<Appointment> nwrapper = new QueryWrapper<>();
        nwrapper.eq("store_id", storeId);
        nwrapper.eq("state", 1);

        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "查询成功");
        msg.put("code", 200);
        Map<String, Object> data = new HashMap<String, Object>();
        msg.put("data", data);
        data.put("rows", timeSlotList);
        data.put("noBillToday", appointmentMapper.selectCount(nwrapper));
        return msg;
    }

    public Map<String, Object> queryStaffBoardAsyncNoPool(String storeId, String arrivalDate) throws InterruptedException {

        QueryWrapper<AppointmentRule> wrapper = new QueryWrapper<>();
        wrapper.eq("store_id", storeId);
        AppointmentRule appointmentRule = appointmentRuleMapper.selectOne(wrapper);

        QueryWrapper<Staff> swrapper = new QueryWrapper<>();
        swrapper.eq("store_id", storeId);
        swrapper.eq("app_situation", 1);
        swrapper.eq("position", "美容师");
        int scount = staffMapper.selectCount(swrapper);
        List<Staff> staffList = staffMapper.selectList(swrapper);

        HashMap<String, Object> tmap = new HashMap<>();
        tmap.put("store_id", storeId);
        List<TemporaryArrange> temporaryArrangeList = temporaryArrangeMapper.selectByMap(tmap);

        QueryWrapper<Appointment> sawrapper = new QueryWrapper<>();
        sawrapper.eq("store_id", storeId);
        sawrapper.eq("arrival_date", arrivalDate);
        sawrapper.ne("state",3);
        List<Appointment> appointmentList = appointmentMapper.selectList(sawrapper);

        QueryWrapper<StaffRest> rwrapper1 = new QueryWrapper<>();
        rwrapper1.eq("store_id", storeId);
        rwrapper1.eq("rest_date", arrivalDate);
        List<StaffRest> staffRestList = staffRestMapper.selectList(rwrapper1);

        List<TimeSlot> timeSlotList = new ArrayList<>();
        for (int j = 9; j <= 18; j++) {
            if (j == 18) {
                TimeSlot timeSlot = new TimeSlot();
                timeSlot.setName(Integer.toString(j) + ":00");
                timeSlotList.add(timeSlot);
            } else {
                String name;
                if (j == 9) {
                    name = "0" + Integer.toString(j);
                } else {
                    name = Integer.toString(j);
                }
                TimeSlot timeSlot = new TimeSlot();
                timeSlot.setName(name + ":00");
                timeSlotList.add(timeSlot);

                TimeSlot timeSlot1 = new TimeSlot();
                timeSlot1.setName(name + ":30");
                timeSlotList.add(timeSlot1);
            }
        }

        ArrayBlockingQueue<TimeSlot> timeSlotQueue = new ArrayBlockingQueue<>(19);
        for (TimeSlot ts : timeSlotList
        ) {
            timeSlotQueue.put(ts);
        }

        CountDownLatch latch = new CountDownLatch(10);
        for (int u = 1; u <= 10; u++) {

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");

            new Thread(new Runnable() {
                @SneakyThrows
                @Override
                public void run() {
                    while (!timeSlotQueue.isEmpty()) {
                        TimeSlot t = null;
                        try {
                            t = timeSlotQueue.take();
                        } catch (InterruptedException e) {
                            latch.countDown();
                            e.printStackTrace();
                        }

                        int taacount = 0;
                        int acount =  0;
                        for (Appointment appointment: appointmentList
                        ) {
                            try {
                                if (appointment.getType() == 2 && (simpleDateFormat.parse(t.getName()).getTime() >= simpleDateFormat.parse(appointment.getServiceStartTime()).getTime() &&
                                        simpleDateFormat.parse(t.getName()).getTime() < simpleDateFormat.parse(appointment.getServiceEndTime()).getTime())){
                                    taacount += 1;
                                    t.getArrangementAppointmentList().add(appointment);
                                }
                            } catch (ParseException e) {
                                latch.countDown();
                                e.printStackTrace();
                            }

                            try {
                                if (appointment.getType() != 3 && (simpleDateFormat.parse(t.getName()).getTime() >= simpleDateFormat.parse(appointment.getServiceStartTime()).getTime() &&
                                        simpleDateFormat.parse(t.getName()).getTime() < simpleDateFormat.parse(appointment.getServiceEndTime()).getTime())){
                                    acount += 1;
                                }
                            } catch (ParseException e) {
                                latch.countDown();
                                e.printStackTrace();
                            }
                        }
                        t.setArrangement(taacount);

                        int rcount = 0;
                        int ecount = 0;
                        List<Map<String, Object>> staffMapList = new ArrayList<>();
                        for (Staff staff: staffList
                        ) {
                            Map<String, Object> staffMap = new HashMap<>();
                            staffMap.put("staffId", staff.getId());
                            staffMap.put("staffName", staff.getName());
                            staffMap.put("currentBoardStatus", 0);
                            staffMap.put("appointment", null);
                            staffMapList.add(staffMap);
                        }
                        for (Map<String, Object> staffMap : staffMapList
                        ) {
                            boolean tem = true;
                            try {
                                for (String staffShiftTime : shiftManagementService.queryStaffShift(storeId, (String) staffMap.get("staffId"), arrivalDate, "2", "1")
                                ) {
                                    if (staffShiftTime.equals(t.getName())) {
                                        tem = false;
                                        break;
                                    }
                                }
                            } catch (ParseException e) {
                                latch.countDown();
                                e.printStackTrace();
                            }
                            if (tem) {
                                rcount += 1;
                                staffMap.put("currentBoardStatus", 2);
                            }

                            for (StaffRest staffRest : staffRestList
                            ) {
                                if (staffRest.getStaffId().equals((String) staffMap.get("staffId")) && staffRest.getRestTime().equals(t.getName())) {
                                    ecount += 1;
                                    staffMap.put("currentBoardStatus", 3);
                                }
                            }

                            if (appointmentRule != null && appointmentRule.getMostMeanwhile() != 0) {
                                if (appointmentRule.getMostMeanwhile() > acount + rcount + ecount) {
                                    t.setResidualReducibility(Integer.toString(appointmentRule.getMostMeanwhile() - rcount - acount - ecount));
                                } else {
                                    t.setResidualReducibility("满");
                                }
                            } else {
                                if (scount > acount + rcount + ecount) {
                                    t.setResidualReducibility(Integer.toString(scount - rcount - acount - ecount));
                                } else {
                                    t.setResidualReducibility("满");
                                }
                            }

                            for (Appointment appointment : appointmentList
                            ) {
                                try {
                                    if (appointment.getBeauticianId().equals((String) staffMap.get("staffId")) && appointment.getType() == 1 && (simpleDateFormat.parse(t.getName()).getTime() >= simpleDateFormat.parse(appointment.getServiceStartTime()).getTime() &&
                                            simpleDateFormat.parse(t.getName()).getTime() < simpleDateFormat.parse(appointment.getServiceEndTime()).getTime())) {

                                        staffMap.put("currentBoardStatus", 1);
                                        staffMap.put("appointment", appointment);

                                        Date start = simpleDateFormat.parse(appointment.getServiceStartTime());
                                        Date end = simpleDateFormat.parse(appointment.getServiceEndTime());
                                        appointment.setOccupiedCells((int) ((end.getTime() - start.getTime()) / 60 / 1000) / 30);
                                    }
                                } catch (ParseException e) {
                                    latch.countDown();
                                    e.printStackTrace();
                                }
                            }

                            if (t.getResidualReducibility().equals("满")) {
                                if ((int) staffMap.get("currentBoardStatus") != 2 && (int) staffMap.get("currentBoardStatus") != 1) {
                                    staffMap.put("currentBoardStatus", 4);
                                }
                            }
                        }
                        t.setStaffMaplist(staffMapList);

                        List<Map<String, Object>> temporaryArrangeMapList = new ArrayList<>();
                        for (TemporaryArrange temporaryArrange: temporaryArrangeList
                        ) {
                            Map<String, Object> temporaryArrangeMap = new HashMap<>();
                            temporaryArrangeMap.put("temporaryArrangeId", temporaryArrange.getId());
                            temporaryArrangeMap.put("temporaryArrangeName", temporaryArrange.getName());
                            temporaryArrangeMap.put("appointment", null);
                            temporaryArrangeMapList.add(temporaryArrangeMap);
                        }
                        for (Map<String, Object> temporaryArrangeMap : temporaryArrangeMapList
                        ) {
                            for (Appointment appointment : appointmentList
                            ) {
                                if (appointment.getTemporaryArrangeId() != null) {
                                    try {
                                        if (appointment.getTemporaryArrangeId().equals((String) temporaryArrangeMap.get("temporaryArrangeId")) && (simpleDateFormat.parse(t.getName()).getTime() >= simpleDateFormat.parse(appointment.getServiceStartTime()).getTime() &&
                                                simpleDateFormat.parse(t.getName()).getTime() < simpleDateFormat.parse(appointment.getServiceEndTime()).getTime())) {
                                            temporaryArrangeMap.put("appointment", appointment);

                                            Date start = simpleDateFormat.parse(appointment.getServiceStartTime());
                                            Date end = simpleDateFormat.parse(appointment.getServiceEndTime());
                                            appointment.setOccupiedCells((int) ((end.getTime() - start.getTime()) / 60 / 1000) / 30);
                                        }
                                    } catch (ParseException e) {
                                        latch.countDown();
                                        e.printStackTrace();
                                    }
                                }
                            }
                        }
                        t.setTemporaryArrangeMapList(temporaryArrangeMapList);

                    }

                    latch.countDown();
                }
            }, "statistical_time_lattice_thread" + String.valueOf(u)).start();
        }

        latch.await();

        QueryWrapper<Appointment> nwrapper = new QueryWrapper<>();
        nwrapper.eq("store_id", storeId);
        nwrapper.eq("state", 1);

        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "查询成功");
        msg.put("code", 200);
        Map<String, Object> data = new HashMap<String, Object>();
        msg.put("data", data);
        data.put("rows", timeSlotList);
        data.put("noBillToday", appointmentMapper.selectCount(nwrapper));
        return msg;
    }

    public Map<String, Object> queryStaffBoardThread(String storeId, String arrivalDate) throws InterruptedException {

        QueryWrapper<AppointmentRule> wrapper = new QueryWrapper<>();
        wrapper.eq("store_id", storeId);
        AppointmentRule appointmentRule = appointmentRuleMapper.selectOne(wrapper);

        QueryWrapper<Staff> swrapper = new QueryWrapper<>();
        swrapper.eq("store_id", storeId);
        swrapper.eq("app_situation", 1);
        swrapper.eq("position", "美容师");
        int scount = staffMapper.selectCount(swrapper);
        List<Staff> staffList = staffMapper.selectList(swrapper);

        HashMap<String, Object> tmap = new HashMap<>();
        tmap.put("store_id", storeId);
        List<TemporaryArrange> temporaryArrangeList = temporaryArrangeMapper.selectByMap(tmap);

        QueryWrapper<Appointment> sawrapper = new QueryWrapper<>();
        sawrapper.eq("store_id", storeId);
        sawrapper.eq("arrival_date", arrivalDate);
        sawrapper.ne("state",3);
        List<Appointment> appointmentList = appointmentMapper.selectList(sawrapper);

        QueryWrapper<StaffRest> rwrapper1 = new QueryWrapper<>();
        rwrapper1.eq("store_id", storeId);
        rwrapper1.eq("rest_date", arrivalDate);
        List<StaffRest> staffRestList = staffRestMapper.selectList(rwrapper1);

        List<TimeSlot> timeSlotList = new ArrayList<>();
        for (int j = 9; j <= 18; j++) {
            if (j == 18) {
                TimeSlot timeSlot = new TimeSlot();
                timeSlot.setName(Integer.toString(j) + ":00");
                timeSlotList.add(timeSlot);
            } else {
                String name;
                if (j == 9) {
                    name = "0" + Integer.toString(j);
                } else {
                    name = Integer.toString(j);
                }
                TimeSlot timeSlot = new TimeSlot();
                timeSlot.setName(name + ":00");
                timeSlotList.add(timeSlot);

                TimeSlot timeSlot1 = new TimeSlot();
                timeSlot1.setName(name + ":30");
                timeSlotList.add(timeSlot1);
            }
        }

        CountDownLatch latch = new CountDownLatch(7);
        for (int i = 0; i < 7; i++) {
            int p = i;
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");

            new Thread(new Runnable() {
                @SneakyThrows
                @Override
                public void run() {
                    List<TimeSlot> subTimeSlotList = new ArrayList<>();
                    if (p >= 6) {
                        subTimeSlotList = timeSlotList.subList(p * 3, timeSlotList.size());
                    } else {
                        subTimeSlotList = timeSlotList.subList(p * 3, (p + 1) * 3);
                    }

                    for (TimeSlot t : subTimeSlotList
                    ) {

                        int taacount = 0;
                        int acount =  0;
                        for (Appointment appointment: appointmentList
                        ) {
                            try {
                                if (appointment.getType() == 2 && (simpleDateFormat.parse(t.getName()).getTime() >= simpleDateFormat.parse(appointment.getServiceStartTime()).getTime() &&
                                        simpleDateFormat.parse(t.getName()).getTime() < simpleDateFormat.parse(appointment.getServiceEndTime()).getTime())){
                                    taacount += 1;
                                    t.getArrangementAppointmentList().add(appointment);
                                }
                            } catch (ParseException e) {
                                latch.countDown();
                                e.printStackTrace();
                            }

                            try {
                                if (appointment.getType() != 3 && (simpleDateFormat.parse(t.getName()).getTime() >= simpleDateFormat.parse(appointment.getServiceStartTime()).getTime() &&
                                        simpleDateFormat.parse(t.getName()).getTime() < simpleDateFormat.parse(appointment.getServiceEndTime()).getTime())){
                                    acount += 1;
                                }
                            } catch (ParseException e) {
                                latch.countDown();
                                e.printStackTrace();
                            }
                        }
                        t.setArrangement(taacount);

                        int rcount = 0;
                        int ecount = 0;
                        List<Map<String, Object>> staffMapList = new ArrayList<>();
                        for (Staff staff: staffList
                        ) {
                            Map<String, Object> staffMap = new HashMap<>();
                            staffMap.put("staffId", staff.getId());
                            staffMap.put("staffName", staff.getName());
                            staffMap.put("currentBoardStatus", 0);
                            staffMap.put("appointment", null);
                            staffMapList.add(staffMap);
                        }
                        for (Map<String, Object> staffMap : staffMapList
                        ) {
                            boolean tem = true;
                            try {
                                for (String staffShiftTime : shiftManagementService.queryStaffShift(storeId, (String) staffMap.get("staffId"), arrivalDate, "2", "1")
                                ) {
                                    if (staffShiftTime.equals(t.getName())) {
                                        tem = false;
                                        break;
                                    }
                                }
                            } catch (ParseException e) {
                                latch.countDown();
                                e.printStackTrace();
                            }
                            if (tem) {
                                rcount += 1;
                                staffMap.put("currentBoardStatus", 2);
                            }

                            for (StaffRest staffRest : staffRestList
                            ) {
                                if (staffRest.getStaffId().equals((String) staffMap.get("staffId")) && staffRest.getRestTime().equals(t.getName())) {
                                    ecount += 1;
                                    staffMap.put("currentBoardStatus", 3);
                                }
                            }

                            if (appointmentRule != null && appointmentRule.getMostMeanwhile() != 0) {
                                if (appointmentRule.getMostMeanwhile() > acount + rcount + ecount) {
                                    t.setResidualReducibility(Integer.toString(appointmentRule.getMostMeanwhile() - rcount - acount - ecount));
                                } else {
                                    t.setResidualReducibility("满");
                                }
                            } else {
                                if (scount > acount + rcount + ecount) {
                                    t.setResidualReducibility(Integer.toString(scount - rcount - acount - ecount));
                                } else {
                                    t.setResidualReducibility("满");
                                }
                            }

                            for (Appointment appointment : appointmentList
                            ) {
                                try {
                                    if (appointment.getBeauticianId().equals((String) staffMap.get("staffId")) && appointment.getType() == 1 && (simpleDateFormat.parse(t.getName()).getTime() >= simpleDateFormat.parse(appointment.getServiceStartTime()).getTime() &&
                                            simpleDateFormat.parse(t.getName()).getTime() < simpleDateFormat.parse(appointment.getServiceEndTime()).getTime())) {

                                        staffMap.put("currentBoardStatus", 1);
                                        staffMap.put("appointment", appointment);

                                        Date start = simpleDateFormat.parse(appointment.getServiceStartTime());
                                        Date end = simpleDateFormat.parse(appointment.getServiceEndTime());

//                                    if (appointment.getOccupiedCells() == 0) {
//                                    reentrantLock.lock();
                                        appointment.setOccupiedCells((int) ((end.getTime() - start.getTime()) / 60 / 1000) / 30);
//                                    reentrantLock.unlock();
//                                    }
                                    }
                                } catch (ParseException e) {
                                    latch.countDown();
                                    e.printStackTrace();
                                }
                            }

                            if (t.getResidualReducibility().equals("满")) {
                                if ((int) staffMap.get("currentBoardStatus") != 2 && (int) staffMap.get("currentBoardStatus") != 1) {
                                    staffMap.put("currentBoardStatus", 4);
                                }
                            }
                        }
                        t.setStaffMaplist(staffMapList);

                        List<Map<String, Object>> temporaryArrangeMapList = new ArrayList<>();
                        for (TemporaryArrange temporaryArrange: temporaryArrangeList
                        ) {
                            Map<String, Object> temporaryArrangeMap = new HashMap<>();
                            temporaryArrangeMap.put("temporaryArrangeId", temporaryArrange.getId());
                            temporaryArrangeMap.put("temporaryArrangeName", temporaryArrange.getName());
                            temporaryArrangeMap.put("appointment", null);
                            temporaryArrangeMapList.add(temporaryArrangeMap);
                        }
                        for (Map<String, Object> temporaryArrangeMap : temporaryArrangeMapList
                        ) {
                            for (Appointment appointment : appointmentList
                            ) {
                                if (appointment.getTemporaryArrangeId() != null) {
                                    try {
                                        if (appointment.getTemporaryArrangeId().equals((String) temporaryArrangeMap.get("temporaryArrangeId")) && (simpleDateFormat.parse(t.getName()).getTime() >= simpleDateFormat.parse(appointment.getServiceStartTime()).getTime() &&
                                                simpleDateFormat.parse(t.getName()).getTime() < simpleDateFormat.parse(appointment.getServiceEndTime()).getTime())) {
                                            temporaryArrangeMap.put("appointment", appointment);

                                            Date start = simpleDateFormat.parse(appointment.getServiceStartTime());
                                            Date end = simpleDateFormat.parse(appointment.getServiceEndTime());

//                                        if (appointment.getOccupiedCells() == 0) {
//                                        reentrantLock.lock();
                                            appointment.setOccupiedCells((int) ((end.getTime() - start.getTime()) / 60 / 1000) / 30);
//                                        reentrantLock.unlock();
//                                        }
                                        }
                                    } catch (ParseException e) {
                                        latch.countDown();
                                        e.printStackTrace();
                                    }
                                }
                            }
                        }
                        t.setTemporaryArrangeMapList(temporaryArrangeMapList);
                    }

                    latch.countDown();
                }
            },"statistical_time_lattice_thread" + String.valueOf(i)).start();
        }
        latch.await();

        QueryWrapper<Appointment> nwrapper = new QueryWrapper<>();
        nwrapper.eq("store_id", storeId);
        nwrapper.eq("state", 1);

        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "查询成功");
        msg.put("code", 200);
        Map<String, Object> data = new HashMap<String, Object>();
        msg.put("data", data);
        data.put("rows", timeSlotList);
        data.put("noBillToday", appointmentMapper.selectCount(nwrapper));
        return msg;
    }

    public Map<String, Object> queryStaffBoardSpringAsync(String storeId, String arrivalDate) throws InterruptedException {

        QueryWrapper<AppointmentRule> wrapper = new QueryWrapper<>();
        wrapper.eq("store_id", storeId);
        AppointmentRule appointmentRule = appointmentRuleMapper.selectOne(wrapper);

        QueryWrapper<Staff> swrapper = new QueryWrapper<>();
        swrapper.eq("store_id", storeId);
        swrapper.eq("app_situation", 1);
        swrapper.eq("position", "美容师");
        int scount = staffMapper.selectCount(swrapper);
        List<Staff> staffList = staffMapper.selectList(swrapper);

        HashMap<String, Object> tmap = new HashMap<>();
        tmap.put("store_id", storeId);
        List<TemporaryArrange> temporaryArrangeList = temporaryArrangeMapper.selectByMap(tmap);

        QueryWrapper<Appointment> sawrapper = new QueryWrapper<>();
        sawrapper.eq("store_id", storeId);
        sawrapper.eq("arrival_date", arrivalDate);
        sawrapper.ne("state",3);
        List<Appointment> appointmentList = appointmentMapper.selectList(sawrapper);

        QueryWrapper<StaffRest> rwrapper1 = new QueryWrapper<>();
        rwrapper1.eq("store_id", storeId);
        rwrapper1.eq("rest_date", arrivalDate);
        List<StaffRest> staffRestList = staffRestMapper.selectList(rwrapper1);

        List<TimeSlot> timeSlotList = new ArrayList<>();
        for (int j = 9; j <= 18; j++) {
            if (j == 18) {
                TimeSlot timeSlot = new TimeSlot();
                timeSlot.setName(Integer.toString(j) + ":00");
                timeSlotList.add(timeSlot);
            } else {
                String name;
                if (j == 9) {
                    name = "0" + Integer.toString(j);
                } else {
                    name = Integer.toString(j);
                }
                TimeSlot timeSlot = new TimeSlot();
                timeSlot.setName(name + ":00");
                timeSlotList.add(timeSlot);

                TimeSlot timeSlot1 = new TimeSlot();
                timeSlot1.setName(name + ":30");
                timeSlotList.add(timeSlot1);
            }
        }

        CountDownLatch latch = new CountDownLatch(7);
        for (int i = 0; i < 7; i++) {
            int p = i;
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");

            boardTask.handleDataSpringAsync(timeSlotList, appointmentList, p, simpleDateFormat, staffList, staffRestList, appointmentRule,
                    storeId, arrivalDate, scount, temporaryArrangeList, latch);
        }
        latch.await();

        QueryWrapper<Appointment> nwrapper = new QueryWrapper<>();
        nwrapper.eq("store_id", storeId);
        nwrapper.eq("state", 1);

        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "查询成功");
        msg.put("code", 200);
        Map<String, Object> data = new HashMap<String, Object>();
        msg.put("data", data);
        data.put("rows", timeSlotList);
        data.put("noBillToday", appointmentMapper.selectCount(nwrapper));
        return msg;
    }

    public Map<String, Object> queryItemStaff(String storeId, String itemId, String nowDate) throws ParseException {

        List<Staff> staffList = staffMapper.queryItemStaff(itemId);
        for (Staff staff :staffList
             ) {
            Map<String, List<TimePoint>> map = shiftManagementService.queryAppletsStaffShift(storeId, staff.getId(), nowDate, "2");

            boolean mtem = true;
            for (TimePoint m :map.get("morning")
                 ) {
                if (!m.isAshDisposal()){
                    mtem = false;
                    break;
                }
            }

            boolean ntem = true;
            for (TimePoint m :map.get("noon")
            ) {
                if (!m.isAshDisposal()){
                    ntem = false;
                    break;
                }
            }

            boolean gtem = true;
            for (TimePoint m :map.get("night")
            ) {
                if (!m.isAshDisposal()){
                    gtem = false;
                    break;
                }
            }

            if (mtem && ntem && gtem){
                staff.setFull(true);
            }

//            if ( map.get("morning").size() == 0 && map.get("noon").size() == 0 && map.get("night").size() == 0){
//                staff.setFull(true);
//            }
        }

        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "查询成功");
        msg.put("code", 200);
        Map<String, Object> data = new HashMap<String, Object>();
        msg.put("data", data);
        data.put("rows", staffList);
        data.put("total", staffList.size());

        return msg;
    }

    public Integer queryBookableNumber(String storeId, String arrivalDate) throws ParseException {

        Integer bookableNumber = 0;
        Integer mostMeanwhile = 0;
        Integer staffTotal = 0;
        Integer rcount = 0;

        QueryWrapper<AppointmentRule> wrapper = new QueryWrapper<>();
        wrapper.eq("store_id", storeId);
        AppointmentRule appointmentRule = appointmentRuleMapper.selectOne(wrapper);

        QueryWrapper<Staff> swrapper = new QueryWrapper<>();
        swrapper.eq("store_id", storeId);
        swrapper.eq("app_situation", 1);
        swrapper.eq("position", "美容师");
        int scount = staffMapper.selectCount(swrapper);
        List<Staff> staffList = staffMapper.selectList(swrapper);

        List<TimeSlot> timeSlotList = new ArrayList<>();
        for (int j = 9; j <= 18; j++) {
            if (j == 18) {
                TimeSlot timeSlot = new TimeSlot();
                timeSlot.setName(Integer.toString(j) + ":00");
                timeSlotList.add(timeSlot);
            } else {
                String name;
                if (j == 9) {
                    name = "0" + Integer.toString(j);
                } else {
                    name = Integer.toString(j);
                }
                TimeSlot timeSlot = new TimeSlot();
                timeSlot.setName(name + ":00");
                timeSlotList.add(timeSlot);

                TimeSlot timeSlot1 = new TimeSlot();
                timeSlot1.setName(name + ":30");
                timeSlotList.add(timeSlot1);
            }
        }
        for (TimeSlot t : timeSlotList
        ) {
            for (Staff staff : staffList
            ) {
                boolean tem = true;
                for (String staffShiftTime : shiftManagementService.queryStaffShift(storeId, staff.getId(), arrivalDate, "2", "1")
                ) {
                    if (staffShiftTime.equals(t.getName())) {
                        tem = false;
                        break;
                    }
                }
                if (tem) {
                    rcount += 1;
                }
            }
            if (appointmentRule != null && appointmentRule.getMostMeanwhile() != 0) {
                 mostMeanwhile += appointmentRule.getMostMeanwhile();
            } else {
                staffTotal += scount;
            }
        }

        if (appointmentRule != null && appointmentRule.getMostMeanwhile() != 0) {
            bookableNumber = mostMeanwhile - rcount;
        } else {
            bookableNumber = staffTotal - rcount;
        }

        return bookableNumber;
    }

    public Integer queryBookableNumberAsync(String storeId, String arrivalDate) throws InterruptedException {

        Integer bookableNumber = 0;
        final Integer[] mostMeanwhile = {0};
        final Integer[] staffTotal = {0};
        final Integer[] rcount = {0};

        QueryWrapper<AppointmentRule> wrapper = new QueryWrapper<>();
        wrapper.eq("store_id", storeId);
        AppointmentRule appointmentRule = appointmentRuleMapper.selectOne(wrapper);

        QueryWrapper<Staff> swrapper = new QueryWrapper<>();
        swrapper.eq("store_id", storeId);
        swrapper.eq("app_situation", 1);
        swrapper.eq("position", "美容师");
        int scount = staffMapper.selectCount(swrapper);
        List<Staff> staffList = staffMapper.selectList(swrapper);

        List<TimeSlot> timeSlotList = new ArrayList<>();
        for (int j = 9; j <= 18; j++) {
            if (j == 18) {
                TimeSlot timeSlot = new TimeSlot();
                timeSlot.setName(Integer.toString(j) + ":00");
                timeSlotList.add(timeSlot);
            } else {
                String name;
                if (j == 9) {
                    name = "0" + Integer.toString(j);
                } else {
                    name = Integer.toString(j);
                }
                TimeSlot timeSlot = new TimeSlot();
                timeSlot.setName(name + ":00");
                timeSlotList.add(timeSlot);

                TimeSlot timeSlot1 = new TimeSlot();
                timeSlot1.setName(name + ":30");
                timeSlotList.add(timeSlot1);
            }
        }

        ReentrantLock reentrantLock = new ReentrantLock();
        CountDownLatch latch = new CountDownLatch(7);
        for (int i = 0; i < 7; i++) {
            int p = i;
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");

            new Thread(new Runnable() {
                @SneakyThrows
                @Override
                public void run() {
                    List<TimeSlot> subTimeSlotList = new ArrayList<>();
                    if (p >= 6) {
                        subTimeSlotList = timeSlotList.subList(p * 3, timeSlotList.size());
                    } else {
                        subTimeSlotList = timeSlotList.subList(p * 3, (p + 1) * 3);
                    }

                    for (TimeSlot t : subTimeSlotList
                    ) {
                        for (Staff staff : staffList
                        ) {
                            boolean tem = true;
                            try {
                                for (String staffShiftTime : shiftManagementService.queryStaffShift(storeId, staff.getId(), arrivalDate, "2", "1")
                                ) {
                                    if (staffShiftTime.equals(t.getName())) {
                                        tem = false;
                                        break;
                                    }
                                }
                            } catch (ParseException e) {
                                latch.countDown();
                                e.printStackTrace();
                            }
                            if (tem) {
                                reentrantLock.lock();
                                rcount[0] += 1;
                                reentrantLock.unlock();
                            }
                        }
                        if (appointmentRule != null && appointmentRule.getMostMeanwhile() != 0) {
                            reentrantLock.lock();
                            mostMeanwhile[0] += appointmentRule.getMostMeanwhile();
                            reentrantLock.unlock();
                        } else {
                            reentrantLock.lock();
                            staffTotal[0] += scount;
                            reentrantLock.unlock();
                        }
                    }

                    latch.countDown();
                }
            },"statistical_number_time_lattice_thread" + String.valueOf(p)).start();
        }
        latch.await();

        if (appointmentRule != null && appointmentRule.getMostMeanwhile() != 0) {
            bookableNumber = mostMeanwhile[0] - rcount[0];
        } else {
            bookableNumber = staffTotal[0] - rcount[0];
        }

        return bookableNumber;
    }

    public Map<String, Object> testTime(String staffId, String arrivalDate, String time) {

        QueryWrapper<Appointment> sawrapper = new QueryWrapper<>();
        sawrapper.eq("beautician_id", staffId);
        sawrapper.eq("type", 1);
        sawrapper.le("complete_service_start_time", arrivalDate + " " + time + ":00");
        sawrapper.gt("complete_service_end_time", arrivalDate + " " + time + ":00");
        sawrapper.ne("state",3);
        List<Appointment> appointmentList = appointmentMapper.selectList(sawrapper);

        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "查询成功");
        msg.put("code", 200);
        Map<String, Object> data = new HashMap<String, Object>();
        msg.put("data", data);
        data.put("rows", appointmentList);
        data.put("total", appointmentList.size());

        return msg;
    }
}
